package com.aletto.general_store.ui.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.splashscreen.SplashScreen;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.aletto.general_store.R;
import com.aletto.general_store.ui.fragments.AuctionFragment;
import com.aletto.general_store.ui.fragments.ChatListFragment;
import com.aletto.general_store.ui.fragments.HomeFragment;
import com.aletto.general_store.ui.fragments.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class MainActivity extends AppCompatActivity
        implements NavigationBarView.OnItemSelectedListener, NavigationBarView.OnItemReselectedListener {

    BottomNavigationView mBottomNavigation;

    final private Map<Integer, Fragment> mFragmentMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SplashScreen.installSplashScreen(this);
        setContentView(R.layout.activity_main);

        mBottomNavigation = findViewById(R.id.bottom_navigation);

        mBottomNavigation.setItemIconTintList(null);
        mBottomNavigation.setOnItemSelectedListener(this);

        // 初始化页面
        if (savedInstanceState != null) {
            restoreFragment(savedInstanceState);

        } else {
            mFragmentMap.put(R.id.nav_home, new HomeFragment());
            mFragmentMap.put(R.id.nav_auction, new AuctionFragment());
            mFragmentMap.put(R.id.nav_chat, new ChatListFragment());
            mFragmentMap.put(R.id.nav_mine, new MineFragment());

            // 默认选中位置
            mBottomNavigation.setSelectedItemId(R.id.nav_home);
        }

        mBottomNavigation.setOnItemReselectedListener(this);
    }

    // 恢复页面
    private void restoreFragment(Bundle savedInstanceState) {
        // 主页
        Fragment homeFragment = getSupportFragmentManager().getFragment(savedInstanceState, HomeFragment.class.getName());
        mFragmentMap.put(R.id.nav_home, homeFragment != null ? homeFragment : new HomeFragment());

        // 我卖出
        Fragment auctionFragment = getSupportFragmentManager().getFragment(savedInstanceState, AuctionFragment.class.getName());
        mFragmentMap.put(R.id.nav_auction, auctionFragment != null ? auctionFragment : new AuctionFragment());

        // 聊天
        Fragment chatListFragment = getSupportFragmentManager().getFragment(savedInstanceState, ChatListFragment.class.getName());
        mFragmentMap.put(R.id.nav_chat, chatListFragment != null ? chatListFragment : new ChatListFragment());

        // 我的
        Fragment mineFragment = getSupportFragmentManager().getFragment(savedInstanceState, MineFragment.class.getName());
        mFragmentMap.put(R.id.nav_mine, mineFragment != null ? mineFragment : new MineFragment());

        // 选择
        mBottomNavigation.setSelectedItemId(savedInstanceState.getInt("SelectedItemId"));
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment targetFragment = mFragmentMap.get(item.getItemId());

        if (targetFragment != null) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!targetFragment.isAdded()) {
                fragmentTransaction.add(R.id.content, targetFragment);
            }

            Set<Map.Entry<Integer, Fragment>> entries = mFragmentMap.entrySet();
            for (Map.Entry<Integer, Fragment> fragmentEntry : entries) {
                Fragment fragmentEntryValue = fragmentEntry.getValue();
                if (fragmentEntryValue == targetFragment) {
                    fragmentTransaction.show(fragmentEntryValue);
                } else {
                    fragmentTransaction.hide(fragmentEntryValue);
                }
            }

            fragmentTransaction.commit();
            return true;
        }

        return true;
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        Set<Map.Entry<Integer, Fragment>> entries = mFragmentMap.entrySet();
        for (Map.Entry<Integer, Fragment> fragmentEntry : entries) {
            Fragment fragment = fragmentEntry.getValue();
            if (fragment.isAdded()) {
                String key = fragment.getClass().getName();
                getSupportFragmentManager().putFragment(outState, key, fragment);
            }
        }

        // 选中的条目
        outState.putInt("SelectedItemId", mBottomNavigation.getSelectedItemId());
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
        Drawable currentIcon = menuItem.getIcon();
        Drawable normalIcon = AppCompatResources.getDrawable(this, R.drawable.selector_nav_home);

        int itemId = menuItem.getItemId();
        if (itemId == R.id.nav_home && currentIcon != normalIcon){
            // 返回
            HomeFragment fragment = (HomeFragment) mFragmentMap.get(R.id.nav_home);
            Objects.requireNonNull(fragment).backTop();
            menuItem.setIcon(normalIcon);
        }
    }


    public void backTop(boolean isBackTop){
        MenuItem menuItem = mBottomNavigation.getMenu().getItem(0);
        if (isBackTop){
            menuItem.setIcon(AppCompatResources.getDrawable(this, R.drawable.selector_nav_home_2));
        } else {
            menuItem.setIcon(AppCompatResources.getDrawable(this, R.drawable.selector_nav_home));
        }
    }

}